<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="总价" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="总价"></el-input>
    </el-form-item>
    <el-form-item label="订单详情" prop="details">
      <el-input v-model="dataForm.details" placeholder="订单详情"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="0取消，1待付款，2已付款,3已发货,4 已完成，5已评价" prop="status">
      <el-input v-model="dataForm.status" placeholder="0取消，1待付款，2已付款,3已发货,4 已完成，5已评价"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="总商品数" prop="totalNum">
      <el-input v-model="dataForm.totalNum" placeholder="总商品数"></el-input>
    </el-form-item>
    <el-form-item label="退款时间" prop="refundTime">
      <el-input v-model="dataForm.refundTime" placeholder="退款时间"></el-input>
    </el-form-item>
    <el-form-item label="1微信支付，2支付宝支付，3qq支付" prop="payType">
      <el-input v-model="dataForm.payType" placeholder="1微信支付，2支付宝支付，3qq支付"></el-input>
    </el-form-item>
    <el-form-item label="1退款申请成功，0，退款申请失败" prop="refundType">
      <el-input v-model="dataForm.refundType" placeholder="1退款申请成功，0，退款申请失败"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="freight">
      <el-input v-model="dataForm.freight" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="收货人" prop="receiver">
      <el-input v-model="dataForm.receiver" placeholder="收货人"></el-input>
    </el-form-item>
    <el-form-item label="收货地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="收货地址"></el-input>
    </el-form-item>
    <el-form-item label="物流编号" prop="logisticsNum">
      <el-input v-model="dataForm.logisticsNum" placeholder="物流编号"></el-input>
    </el-form-item>
    <el-form-item label="物流方式" prop="logisticsType">
      <el-input v-model="dataForm.logisticsType" placeholder="物流方式"></el-input>
    </el-form-item>
    <el-form-item label="0 到店自取 1快递" prop="sendstyle">
      <el-input v-model="dataForm.sendstyle" placeholder="0 到店自取 1快递"></el-input>
    </el-form-item>
    <el-form-item label="0：未删除1：已删除" prop="isDel">
      <el-input v-model="dataForm.isDel" placeholder="0：未删除1：已删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderNumber: '',
          userId: '',
          totalPrice: '',
          details: '',
          createTime: '',
          status: '',
          mobile: '',
          payTime: '',
          totalNum: '',
          refundTime: '',
          payType: '',
          refundType: '',
          remark: '',
          freight: '',
          receiver: '',
          address: '',
          logisticsNum: '',
          logisticsType: '',
          sendstyle: '',
          isDel: ''
        },
        dataRule: {
          orderNumber: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '总价不能为空', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '订单详情不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '0取消，1待付款，2已付款,3已发货,4 已完成，5已评价不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          totalNum: [
            { required: true, message: '总商品数不能为空', trigger: 'blur' }
          ],
          refundTime: [
            { required: true, message: '退款时间不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '1微信支付，2支付宝支付，3qq支付不能为空', trigger: 'blur' }
          ],
          refundType: [
            { required: true, message: '1退款申请成功，0，退款申请失败不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          freight: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          receiver: [
            { required: true, message: '收货人不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' }
          ],
          logisticsNum: [
            { required: true, message: '物流编号不能为空', trigger: 'blur' }
          ],
          logisticsType: [
            { required: true, message: '物流方式不能为空', trigger: 'blur' }
          ],
          sendstyle: [
            { required: true, message: '0 到店自取 1快递不能为空', trigger: 'blur' }
          ],
          isDel: [
            { required: true, message: '0：未删除1：已删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNumber = data.order.orderNumber
                this.dataForm.userId = data.order.userId
                this.dataForm.totalPrice = data.order.totalPrice
                this.dataForm.details = data.order.details
                this.dataForm.createTime = data.order.createTime
                this.dataForm.status = data.order.status
                this.dataForm.mobile = data.order.mobile
                this.dataForm.payTime = data.order.payTime
                this.dataForm.totalNum = data.order.totalNum
                this.dataForm.refundTime = data.order.refundTime
                this.dataForm.payType = data.order.payType
                this.dataForm.refundType = data.order.refundType
                this.dataForm.remark = data.order.remark
                this.dataForm.freight = data.order.freight
                this.dataForm.receiver = data.order.receiver
                this.dataForm.address = data.order.address
                this.dataForm.logisticsNum = data.order.logisticsNum
                this.dataForm.logisticsType = data.order.logisticsType
                this.dataForm.sendstyle = data.order.sendstyle
                this.dataForm.isDel = data.order.isDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderNumber': this.dataForm.orderNumber,
                'userId': this.dataForm.userId,
                'totalPrice': this.dataForm.totalPrice,
                'details': this.dataForm.details,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status,
                'mobile': this.dataForm.mobile,
                'payTime': this.dataForm.payTime,
                'totalNum': this.dataForm.totalNum,
                'refundTime': this.dataForm.refundTime,
                'payType': this.dataForm.payType,
                'refundType': this.dataForm.refundType,
                'remark': this.dataForm.remark,
                'freight': this.dataForm.freight,
                'receiver': this.dataForm.receiver,
                'address': this.dataForm.address,
                'logisticsNum': this.dataForm.logisticsNum,
                'logisticsType': this.dataForm.logisticsType,
                'sendstyle': this.dataForm.sendstyle,
                'isDel': this.dataForm.isDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
